#include "User.h"
#include "ServerRight.h"
#include "Configuration.h"

User::User(const unsigned id, const std::string& login, const std::string& password)
: _id(id), _login(login), _password(password), _currentChannel(NULL), _currentChannelRight(NULL)
{
	//Database::DbResult	result;

	///*std::map<std::string, IDbValue*>::iterator	idMapBegin = idMap.begin;
	//std::map<std::string, IDbValue*>::iterator	idMapEnd = idMap.end;*/
	//std::stringstream	query;
	//query << "SELECT id FROM main.users WHERE login=\"" << login << "\";"; //a revoir, pas de gestion de pass, peut etre se passer du stream
	//Database::GetDatabase()->Execute(query.str(), result);
	///*for (; idMapBegin != idMapEnd; ++idMapBegin)
	//{
	//	if ()
	//}*/
	//if (result.size() == 1)
	//	this->_id = result[0]->begin()->second->GetValueInt();
	////else //emettre une error
	_serverRight = new ServerRight(); //get _serverRight par defaut
}

User::~User(void)
{
}

const unsigned int	User::GetId() const
{
	return this->_id;
}

// ne devrait pas exister
//const unsigned int	User::SetId(unsigned int id)
//{
//	this->_id = id;
//	return 0;
//}

const std::string&	User::GetLogin() const
{
	return this->_login;
}

const unsigned int	User::SetLogin(const std::string& login)
{
	//changement dans la base
	Configuration::GetConfiguration()->GetDatabase()->UpdateUserLogin(this->_id, login);
	this->_login = login;
	return 0;
}

const std::string&	User::GetPassword() const
{
	return this->_password;
}

const unsigned int	User::SetPassword(const std::string& password)
{
	//changement dans la base
	Configuration::GetConfiguration()->GetDatabase()->UpdateUserPassword(this->_id, password);
	this->_password = password;
	return 0;
}

IChannel*		User::GetCurrentChannel() const
{
	return this->_currentChannel;
}

const unsigned int	User::SetCurrentChannel(IChannel* channel)
{
	this->_currentChannel = channel;
	return 0;
}

/*
const std::vector<IChannel*>&	User::GetChannel() const
{
	return this->_channel;
}
*/

//const unsigned int	User::SubscribeChannel(IChannel* channel, const IChannelRight& right)
//{
//	//ajout dans la base
//	return 0;
//}

IServerRight*	User::GetServerRight() const
{
	return this->_serverRight;
}

const unsigned int	User::SetNoCurrentChannel(void)
{
	this->_currentChannel = NULL;
	if (this->_currentChannelRight)
		delete this->_currentChannelRight;
	this->_currentChannelRight = NULL;
	return 0;
}